package com.example.controller.mobilecontroller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.config.ResponseVO;
import com.example.pojo.Classes;
import com.example.pojo.EnrollInfo;
import com.example.pojo.StuInfo;
import com.example.pojo.User;
import com.example.service.EnrollInfoService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/mobile")
public class EnrollInfoController {
    @Resource
    private EnrollInfoService enrollInfoService;

    @GetMapping("/enrollInfoList")
    public ResponseEntity<ResponseVO<List<EnrollInfo>>> getEnrollInfoList() {
        List<EnrollInfo> allEnroll = enrollInfoService.getAllEnroll();
        if (!allEnroll.isEmpty()) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "报名信息获取成功", allEnroll));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "暂无用户数据", null));
    }

    @GetMapping("/enrollInfoListPage/{currentPage}/{limit}")
    public ResponseEntity<ResponseVO<IPage<EnrollInfo>>> getEnrollInfoPage(@PathVariable int currentPage, @PathVariable int limit) {
        IPage<EnrollInfo>  enrollInfoIPage = enrollInfoService.getAllEnrollPage(currentPage, limit);
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "用户信息获取成功", enrollInfoIPage));
    }

    @GetMapping("/enrollInfoListPage/{currentPage}/{limit}/{enrollTitle}")
    public ResponseEntity<ResponseVO<IPage<EnrollInfo>>> getEnrollInfoPage(@PathVariable int currentPage, @PathVariable int limit,@PathVariable String enrollTitle) {
        IPage<EnrollInfo>  enrollInfoIPage = enrollInfoService.getEnrollByTitle(currentPage, limit, enrollTitle);
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "用户信息获取成功", enrollInfoIPage));
    }

    @PostMapping("/stuEnroll/{enrollId}")
    public ResponseEntity<ResponseVO<String>> stuEnroll(@RequestBody StuInfo stuInfo, @PathVariable Integer enrollId) {
        // 首先需要测试是否该学生的年龄是否符合报名的适龄
        System.out.println(stuInfo);
        String bindCode = enrollInfoService.stuEnroll(stuInfo, enrollId);
        if(!bindCode.isEmpty()){
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK,"您的绑定码是",bindCode));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "报名错误,请稍后再试", null));
    }

    @PostMapping("/addEnrollInfo")
    public ResponseEntity<ResponseVO<String>> addEnroll(@RequestBody EnrollInfo enrollInfo){
        System.out.println(enrollInfo.getEnrollEndTime());
        int i = enrollInfoService.addEnroll(enrollInfo);
        if (i > 0) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "用户添加成功", "success"));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.NOT_FOUND, "系统繁忙，请稍后再试", null));
    }

    @DeleteMapping("/batchDeleteEnrollInfo")
    public ResponseEntity<ResponseVO<String>> batchDeleteEnrollInfo(@RequestParam List<Integer> enrollInfoArr){
        System.out.println(enrollInfoArr);
//        try {
//            int result = enrollInfoService.batchDeleteEnrollInfo(enrollInfoArr);
//            if(result>0){
//                return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK,"删除成功"));
//            }else{
//                return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST,"删除用户失败,请稍后再试"));
//            }
//        } catch (Exception e) {
//            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST,"删除用户失败 " + e.getMessage()));
//        }
        return null;
    }

    @PostMapping("/addOrUpdateEnrollInfo")
    public ResponseEntity<ResponseVO<String>> addOrUpdateEnrollInfo(@RequestBody EnrollInfo enrollInfo) {
        String result = enrollInfoService.addOrUpdateEnrollInfo(enrollInfo);
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK,result));
    }

    @PostMapping("/deleteEnrollInfoById/{deleteId}")
    public ResponseEntity<ResponseVO<String>> deleteEnrollInfo(@PathVariable("deleteId") int deleteId) {
        int resultRow = enrollInfoService.deleteEnrollInfo(deleteId);
        if (resultRow > 0) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "用户删除成功", "success"));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.NOT_FOUND, "系统繁忙，请稍后再试", null));
    }
}
